﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.students
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        student stud = new student();
        Classes cl = new Classes();
        user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 1950; i <= 2050; i++)
                {
                    ddyearstuding.Items.Add(i.ToString()); ddyear.Items.Add(i.ToString());
                }
                for (int j = 1; j <= 31; j++)
                    dddate.Items.Add(j.ToString());
                DataTable dt2 = cl.GetRecords(-99, "", -99, false, " and ");
                ddclass.DataSource = dt2;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();

                DataTable dt3 = ut.GetRecords(-99, "", false, " and ");
                ddusertype.DataSource = dt3;
                ddusertype.DataTextField = "user_type";
                ddusertype.DataValueField = "user_type_id";
                ddusertype.DataBind();

                Panel1.Visible = false;
                DataTable dt = stud.GetRecords(Convert.ToInt64(Session["userid"]), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
                DetailsView1.DataSource = dt;
                DetailsView1.DataBind();

            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            Panel1.Visible = true; Panel2.Visible = false;
            DetailsView1.Visible = false;
            DataTable dt = stud.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
            tbstudentid.Text = dt.Rows[0]["student_id"].ToString();
            tbenroll.Text = dt.Rows[0]["enrollment_no"].ToString();
            tbroll.Text = dt.Rows[0]["roll_no"].ToString();
            DateTime dat1 = new DateTime();
            dat1= Convert.ToDateTime(dt.Rows[0]["year_of_studing"].ToString());
            ddyearstuding.SelectedValue = dat1.Year.ToString();
            tbstudname.Text = dt.Rows[0]["student_name"].ToString();
            DateTime dat = new DateTime();
            dat = Convert.ToDateTime(dt.Rows[0]["date_of_birth"].ToString());
            dddate.SelectedValue = dat.Day.ToString();
            ddmonth.SelectedValue = dat.Month.ToString();
            ddyear.SelectedValue = dat.Year.ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["gender"].ToString();
            TextBox1.Text = dt.Rows[0]["photo"].ToString();
            Image2.ImageUrl = "~/admin/student/" + TextBox1.Text;
            tbpaddess.Text = dt.Rows[0]["permanent_address"].ToString();
            tbrespno.Text = dt.Rows[0]["residence_pno"].ToString();
            tbmobile.Text = dt.Rows[0]["mobile_no"].ToString();
            tbstudmail.Text = dt.Rows[0]["student_emailid"].ToString();
            ddusertype.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            tbusername.Text = dt.Rows[0]["username"].ToString();
            tbpassword.Text = dt.Rows[0]["password"].ToString();
            ddquestion.Text = dt.Rows[0]["security_question"].ToString();
            tbanswer.Text = dt.Rows[0]["answer"].ToString();
        }

        protected void dddate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddmonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddyear_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddyearstuding_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddclass_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddusertype_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            stud.student_id = Convert.ToInt64(tbstudentid.Text);
            stud.enrollment_no = tbenroll.Text;
            stud.roll_no = Convert.ToInt64(tbroll.Text);
            stud.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            stud.year_of_studing = ddyearstuding.SelectedValue.ToString();
            stud.student_name = tbstudname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            stud.date_of_birth = dat.ToString();
            stud.gender = RadioButtonList1.SelectedValue.ToString();
            if (FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/student/" + FileUpload1.FileName));
                stud.photo = FileUpload1.FileName;
            }
            else
                stud.photo = TextBox1.Text;
            stud.permanent_address = tbpaddess.Text;
            stud.residence_pno = Convert.ToInt64(tbrespno.Text);
            stud.mobile_no = Convert.ToInt64(tbmobile.Text);
            stud.student_emailid = tbstudmail.Text;
            stud.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
            stud.username = tbusername.Text;
            stud.password = tbpassword.Text;
            stud.security_question = ddquestion.SelectedValue.ToString();
            stud.answer = tbanswer.Text;
            stud.isdeleted = false;
            stud.SaveRecords();
            Response.Redirect("~/students/SProfile.aspx");

        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {

        }

        protected void ddquestion_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

         
    }
}
